<template>
	<view>
		<!--pages/connectDetail/deviceDetail.wxml-->
		<view class="bluetooth-detail">
			<view>当前连接的蓝牙设备是：</view>
			<view v-for="(item, index) in deviceList" class="device-list">
				<view>设备名：{{item.deviceName}}</view>
				<view>连接状态：{{item.connected? '已连接':'已断开'}}</view>
				<view>设备ID：{{item.deviceId}}</view>˚
			</view>

		</view>
		<view class="card">
			<view>日志(可滑动查看)：</view>
			<button class="btn" @tap="clearLogs">清空日志</button>
			<view>
				<scroll-view scroll-y="true" class="text-box list">
					<text style="word-break: break-all;">{{textLog}}</text>
				</scroll-view>
			</view>
		</view>
		<view class="opt-group">
			<view class="opt-btn-block">
				<button class="btn" @tap="unbindDevice">解除设备绑定</button>
			</view>
		</view>
	</view>
</template>

<script>
	// pages/connectDetail/deviceDetail.js
	var app = getApp();
	import sdkAction from "mcbluetoothsdk";
	// var sdkAction = require("../../src/action");

	export default {
		data() {
			return {
				textLog: "",
				deviceId: "",
				timer: null,
				deviceList: [],
				name: ""
			};
		},

		components: {},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			this.openBluetoothAdapter()
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			let that = this
			if (uni.setKeepScreenOn) {
				uni.setKeepScreenOn({
					keepScreenOn: true,
					success: function(res) {
						console.log('正保持屏幕常亮');
					}
				});
			}
			uni.showToast({
				title: '连接中...',
				icon: 'loading',
				mask: true,
				duration: 3000
			});
			sdkAction.dispatchConnectState(function(res) {
				console.log('当前设备状态', res)
				that.deviceList = []
				that.setData({
					deviceList: res
				})
			})
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {
			
		},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {
			uni.closeBluetoothAdapter({
				success(res) {
					console.log(res)
				}
			})
		},
		methods: {
			clearLogs: function() {
				this.setData({
					textLog: ""
				});
			},
			// 初始化小程序蓝牙模块
			openBluetoothAdapter: function() {
				var that = this;
				uni.openBluetoothAdapter({
					success: function(res) {
						uni.showToast({
							title: '打开蓝牙适配器成功！',
							icon: 'none',
							duration: 1500
						});
						var bindDeviceList = app.globalData.bindDeviceList;
						bindDeviceList.map((v) => {
							sdkAction.doInitMulti(v.deviceUUId, function() {
								sdkAction.startMultiConnectDevice(v.deviceType, v
									.deviceUUId,
									function(res) {
										console.log(22, res)
										var log = that.textLog + JSON.stringify(
											res) + "\n";
										that.setData({
											textLog: log
										})
									})
							})
						})
						var log = that.textLog + "打开蓝牙适配器成功！\n";
						that.setData({
							textLog: log,
							isBlueToothOpen: true
						});
					},
					fail: function(err) {
						var log = that.textLog + "蓝牙开关未开启 \n";
						that.setData({
							textLog: log,
							isBlueToothOpen: false
						});
						uni.showModal({
							title: '提示',
							content: "蓝牙开关未开启",
							showCancel: false
						});
					}
				}); // 监听蓝牙适配器状态变化事件

				uni.onBluetoothAdapterStateChange(function(res) {
					console.log(res)

				});
			},
			closeConnection: function() {
				sdkAction.stopConnectDevice(this.deviceId, function() {
					uni.navigateTo({
						url: '/pages/selectDevice/startpage'
					});
				}, true);
			},
			unbindDevice: function() {
				var that = this;
				sdkAction.closeBlueTooth(function(res) {
					console.log('t', res)
					var appBindDeviceList = app.globalData.bindDeviceList;

					if (appBindDeviceList.length) {
						if (appBindDeviceList.length === 1) {
							app.globalData.bindDeviceList = [];
						} else {
							var deleteIdx = -1;

							for (let i = 0; i < appBindDeviceList.length; i++) {
								if (appBindDeviceList[i].deviceUUId === that.deviceId) {
									deleteIdx = i;
								}
							}

							app.globalData.bindDeviceList.splice(deleteIdx, 1);
						}
					}

					uni.navigateTo({
						url: '/pages/selectDevice/startpage'
					});
				});
			}
		}
	};
</script>
<style>
	/* pages/connectDetail/deviceDetail.wxss */
	.device-list {}

	.bluetooth-detail {
		width: 95%;
		margin: 0 auto;
		padding: 10px 20px;
		box-sizing: border-box;
		border-bottom: 1px solid #ddd;
	}

	.card {
		border-radius: 10rpx;
		background-color: #fff;
		margin: 20rpx;
		padding: 20rpx;
		min-height: 500rpx;
		box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, .3);
	}

	.list {
		max-height: 45vh;
	}

	.opt-input {
		width: 80%;
		position: relative;
		font-size: 16rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		margin: 0 10%;
	}

	.input {
		border: 2rpx solid #ccc;
		width: 400rpx;
		padding: 16rpx 20rpx;
		font-size: 32rpx;
	}

	.opt-group {
		width: 100%;
		position: absolute;
		bottom: 6%;
		font-size: 16rpx;
	}

	.opt-btn-block {
		margin-top: 60rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	.opt-btn-block .btn {
		width: 300rpx;
		float: left;
	}
</style>
